package com.soft.collateral.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.Edger;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.EdgerService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 押品台账表 前端控制器
 * </p>
 *
 * @author liubin
 * @since 2025-08-03
 */
@Slf4j
@Api(tags = "后台系统-押品台账信息")
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/edger")
public class EdgerController {

    private final EdgerService edgerService;

    @ApiOperation("分页查询台账")
    @PostMapping("page")
    public Result page(@RequestBody PageQueryVO vo) {

        Page<Edger> ontractsPage = edgerService.page(vo);
        if(ontractsPage!=null && ontractsPage.getTotal()>0){
            return new Result(true , MessageConstant.GET_EDGER_SUCCESS , ontractsPage);
        }
        return new Result(false , MessageConstant.GET_EDGER_FAIL);
    }

    @ApiOperation("新增台账")   //若已存在该押品id不允许增加
    @PostMapping("create")
    public Result create(@RequestBody Edger edger) {

        boolean flag = edgerService.saveEdger(edger);
        if(flag){
            return new Result(true , MessageConstant.SAVE_EDGER_SUCCESS);
        }
        return new Result(false , MessageConstant.SAVE_EDGER_FAIL);
    }

    @ApiOperation("根据id查询台账")
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {

        Edger edger = edgerService.getById(id);
        if(edger!=null){
            return new Result(true , MessageConstant.GET_EDGER_SUCCESS , edger);
        }
        return new Result(false , MessageConstant.GET_EDGER_FAIL);

    }

    @ApiOperation("根据押品id查询台账")
    @GetMapping("findByCollateralId/{id}")
    public Result findByCollateralId(@PathVariable Integer id) {

        Edger edger = edgerService.findByCollateralId(id);
        if(edger!=null){
            return new Result(true , MessageConstant.GET_EDGER_SUCCESS , edger);
        }
        return new Result(false , MessageConstant.GET_EDGER_FAIL);

    }

    @ApiOperation("根据id删除台账")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {

        boolean flag = edgerService.removeById(id);
        if(flag){
            return new Result(true ,MessageConstant.DELETE_EDGER_SUCCESS);
        }
        return new Result(false ,MessageConstant.DELETE_EDGER_FAIL);
    }

    @ApiOperation("更新台账信息")
    @PutMapping("update")
    public Result update(@RequestBody Edger edger) {

        boolean flag = edgerService.updateById(edger);
        if(flag){
            return new Result(true ,MessageConstant.UPDATE_EDGER_SUCCESS);
        }
        return new Result(false ,MessageConstant.UPDATE_EDGER_FAIL);
    }
}
